﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TechMadeira.Core.Command
{
    public class MacroCommand : ICommand
    {
        public MacroCommand()
        {
            Commands = new List<ICommand>();
        }

        private IList<ICommand> Commands { get; set; }

        public void Append( ICommand aCommand )
        {
            Commands.Add( aCommand );
        }

        public void Remove( ICommand aCommand )
        {
            if ( ! Commands.Contains( aCommand ) ) return;
            Commands.Remove( aCommand );
        }

        public void Execute()
        {
            foreach ( var command in Commands )
            {
                command.Execute();
            }
        }

        public void Undo()
        {
            for ( int i = Commands.Count - 1 ; i > -1 ; i-- )
            {
                Commands[i].Undo();
            }
        }

        public void Redo()
        {
            foreach ( var command in Commands )
            {
                command.Redo();
            }
        }

        public void Abort()
        {
            foreach ( var command in Commands.Reverse() )
            {
                command.Abort();
            }
        }
    }
}
